package com.yunsom.testdemo.multiple_thread.producer_customer.multiple_thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class Consumer extends Thread{
    private BlockingQueue<Integer> buffer;

    public Consumer(BlockingQueue<Integer> buffer) {
        this.buffer = buffer;
    }

    @Override
    public void run() {
        while (true){
            for (int i = 0; i < 10; i++) {
                try {
                    //blockingqueue的take方法会在没有数据的时候阻塞等待
                    System.out.println("仓库当前待处理数量" + buffer.size());
                    System.out.println(Thread.currentThread().getName() + "消费数据" + buffer.take());
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
